package com.ilop.sthome.ui.activity.config;

import android.content.Intent;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.example.xmpic.ui.activity.bind.CameraGuideActivity;
import com.example.xmpic.ui.activity.bind.CameraConfigScanActivity;
import com.example.xmpic.ui.activity.bind.CameraConfigInLanActivity;
import com.ilop.sthome.ui.adapter.room.RoomChooseAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityConfigCameraBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-08.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：配置摄像头和房间信息
 */
public class ConfigCameraActivity extends BaseBindActivity<ActivityConfigCameraBinding> {

    private int mPosition = 0;
    private int addCameraType;
    private RoomChooseAdapter mRoomAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_config_camera;
    }

    @Override
    protected void initialize() {
        super.initialize();
        addCameraType = getIntent().getIntExtra("addType", 0);
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etCameraName);
    }

    @Override
    protected void initData() {
        super.initData();
        int cameraSize = DeviceDaoUtil.getInstance().findAllCamera().size() + 1;
        String camera = getString(R.string.ali_camera) + cameraSize;
        mDBind.etCameraName.setText(camera);
        getRoomList();

        LiveDataBus.get().with(CommonId.CHOOSE_ROOM, Integer.class).observe(this , integer -> {
            this.mPosition = integer;
            mRoomAdapter.setPosition(integer);
        });
    }

    private void getRoomList() {
        List<String> mNameList = RoomDaoUtil.getInstance().findRoomName(mContext);
        GridLayoutManager mManager = new GridLayoutManager(mContext, 4);
        mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return ConfigureUtil.gridLayoutLineNum(mNameList.get(i).length());
            }
        });
        mRoomAdapter = new RoomChooseAdapter(mContext, mNameList);
        mDBind.configRoomList.setLayoutManager(mManager);
        mDBind.configRoomList.setAdapter(mRoomAdapter);
        mRoomAdapter.setPosition(mPosition);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivConfigBack.setOnClickListener(view -> finish());

        mDBind.configGatewayNext.setOnClickListener(view -> {
            String mCameraName = mDBind.etCameraName.getText().toString();
            if (TextUtils.isEmpty(mCameraName)){
                showToast(getString(R.string.ali_rename_camera_name));
            }else {
                List<RoomBean> mList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
                String mRoomId = mList.get(mPosition).getRid();
                SpUtil.putString(mContext, CommonId.KEY_BIND_ROOM, mRoomId);
                SpUtil.putString(mContext, CommonId.KEY_BIND_CAMERA, mCameraName);
                Intent intent = new Intent();
                switch (addCameraType) {
                    case 1:
                        intent.setClass(mContext, CameraGuideActivity.class);
                        break;
                    case 2:
                        intent.setClass(mContext, CameraConfigScanActivity.class);
                        break;
                    case 3:
                        intent.setClass(mContext, CameraConfigInLanActivity.class);
                        break;
                }
                startActivity(intent);
                finish();
            }
        });
    }
}
